package com.onlinelearn.notifyservice.controller;

import com.onlinelearn.notifyservice.dto.NotificationDTO;
import com.onlinelearn.notifyservice.service.NotificationService;
import com.onlinelearn.notifyservice.model.UserPreference;
import com.onlinelearn.notifyservice.service.UserPreferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/notifications")
public class NotificationController {
    @Autowired
    private NotificationService notificationService;

    @Autowired
    private UserPreferenceService userPreferenceService;

    @GetMapping("/unread/{userId}")
    public ResponseEntity<List<NotificationDTO>> getUnreadNotifications(@PathVariable Long userId) {
        List<NotificationDTO> notifications = notificationService.getUnreadNotifications(userId);
        return ResponseEntity.ok(notifications);
    }

    @PostMapping
    public ResponseEntity<NotificationDTO> createNotification(@RequestBody NotificationDTO notificationDTO) {
        NotificationDTO savedNotification = notificationService.createNotification(notificationDTO);
        return ResponseEntity.ok(savedNotification);
    }

    @PutMapping("/read/{notificationId}")
    public ResponseEntity<Void> markNotificationAsRead(@PathVariable Long notificationId) {
        notificationService.markNotificationAsRead(notificationId);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/preferences")
    public ResponseEntity<Void> updateNotificationPreferences(
        @RequestParam Long userId,
        @RequestBody Map<NotificationType, UserPreference.NotificationChannel> preferences
    ) {
        userPreferenceService.updateNotificationPreferences(userId, preferences);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/disable")
    public ResponseEntity<Void> disableNotificationType(
        @RequestParam Long userId,
        @RequestParam NotificationType type
    ) {
        userPreferenceService.disableNotificationType(userId, type);
        return ResponseEntity.ok().build();
    }
} 